package com.aaa.anju.servlet;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

@WebServlet("/area/*")
public class StudentListServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        //   /anju/area/haha   /anju/area/hhehe   /anju/area/xixi
        String requestURI = req.getRequestURI();

        String[] split = requestURI.split("/");
        String path = split[ split.length - 1 ];

        if( path.equals("list")  ){
            list(req,resp);
        }else if(path.equals("add")){
            add(req,resp);
        }else if(path.equals("delStu")){
            del(req,resp);
        }else if(path.equals("getOne")){
            getOne(req,resp);
        }else if(path.equals("update")){

        }
    }
    protected void getOne(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String id = req.getParameter("id");
        try {
            Entity entity = Db.use().get("areainfo", "areaid", id);
            req.setAttribute("a",entity);
            req.getRequestDispatcher("/update.jsp").forward(req,resp);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    protected void add(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        // 1 获取当前请求参数
        String areaname = req.getParameter("areaname");
        // 2 如果添加的名字已经存在 则不让添加 【唯一性校验】 查询 areaname 在数据库中是否存在
        // 3 添加地区
        try {
            int insert = Db.use().insert(Entity.create("areainfo").set("areaname", areaname));
            req.setAttribute("msg", insert > 0  ? "添加成功" : "添加失败"      );
            req.getRequestDispatcher("/add.jsp").forward(req,resp);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


    protected void list(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        try {
            // 1 获取所有的信息
            List<Entity> entityList = Db.use().findAll("areainfo");
            // 2 共享数据
            req.setAttribute(  "data" ,  entityList   );
            // 3 跳转到jsp
            req.getRequestDispatcher("/list.jsp").forward(req,resp);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    protected void del(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        //1 接收请求参数
        String id = req.getParameter("id");
        try {
            //2 根据id删除数据
            Db.use().del("areainfo", "areaid", id);

            //3 刷新主界面  重定向技术
            resp.sendRedirect("/anju/area/list");

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
